﻿@model NewsLetterSubscriptionListModel
@inject AdminAreaSettings adminAreaSettings
@{
    //page title
    ViewBag.Title = T("Admin.Promotions.newsLetterSubscriptions").Text;
}
<form asp-controller="NewsletterSubscription" asp-action="List" method="post">
    

    <div class="row">
        <div class="col-md-12">
            <div class="x_panel light form-fit">
                <div class="x_title">
                    <div class="caption">
                        <i class="fa fa-envelope"></i>
                        @T("Admin.Promotions.NewsLetterSubscriptions")
                    </div>
                    <div class="actions btn-group btn-group-devided">
                        <input type="submit" name="exportcsv" class="btn default" value="@T("Admin.Common.ExportToCsv")" />
                        <input type="submit" id="importcsv" name="importcsv" class="btn default" value="@T("Admin.Common.ImportFromCsv")" />
                        <vc:admin-widget widget-zone="newsletter_subscription_list_buttons" additional-data="null" />
                    </div>
                </div>
                <div class="x_content form">
                    <div class="form-horizontal">
                        <div class="form-body">
                            <div class="form-group">
                                <admin-label asp-for="SearchEmail" />
                                <div class="col-md-9 col-sm-9">
                                    <admin-input asp-for="SearchEmail" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="ActiveId" />
                                <div class="col-md-9 col-sm-9">
                                    <admin-select asp-for="ActiveId" asp-items="Model.ActiveList" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="StoreId" />
                                <div class="col-md-9 col-sm-9">
                                    <admin-select asp-for="StoreId" asp-items="Model.AvailableStores" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchCategoryIds" />
                                <div class="col-xs-4 col-md-8 col-sm-8">
                                    <admin-input asp-for="SearchCategoryIds" asp-template="MultiSelect" asp-selectitem="Model.AvailableCategories" />
                                </div>
                            </div>
                        </div>
                        <div class="form-actions">
                            <div class="row">
                                <div class="col-md-offset-3 col-sm-offset-3 col-md-9 col-sm-9">
                                    <button class="btn btn-success filter-submit" id="search-subscriptions">
                                        <i class="fa fa-search"></i> @T("Admin.Common.Search")
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="x_content">
                    <div id="newsletter-subscriptions-grid"></div>
                </div>
            </div>
        </div>
    </div>


    <script>
        $(document).ready(function() {
            $("#newsletter-subscriptions-grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("SubscriptionList", "NewsLetterSubscription"))",
                            type: "POST",
                            dataType: "json",
                            data: additionalData
                        },
                        update: {
                            url: "@Html.Raw(Url.Action("SubscriptionUpdate", "NewsLetterSubscription"))",
                            type: "POST",
                            dataType: "json",
                            data: addAntiForgeryToken
                        },
                        destroy: {
                            url: "@Html.Raw(Url.Action("SubscriptionDelete", "NewsLetterSubscription"))",
                            type: "POST",
                            dataType: "json",
                            data: addAntiForgeryToken
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors",
                        model: {
                            id: "Id",
                            fields: {
                                Email: { editable: true, type: "string" },
                                Active: { editable: true, type: "boolean" },
                                StoreName: { editable: false, type: "string" },
                                CreatedOn: { editable: false, type: "string" },
                                Categories: { editable: false, type: "string"}
                            }
                        }
                    },
                    requestEnd: function(e) {
                        if (e.type == "update") {
                            this.read();
                        }
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    pageSize: @(adminAreaSettings.DefaultGridPageSize),
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                pageable: {
                    refresh: true,
                    pageSizes: [@(adminAreaSettings.GridPageSizes)]
                },
                editable: {
                    confirmation: false,
                    mode: "inline"
                },
                scrollable: false,
                columns: [
                    {
                        field: "Email",
                        title: "@T("Admin.Promotions.NewsLetterSubscriptions.Fields.Email")",
                        width: 300
                    }, {
                        field: "Active",
                        title: "@T("Admin.Promotions.NewsLetterSubscriptions.Fields.Active")",
                        width: 100,
                        headerAttributes: { style: "text-align:center" },
                        attributes: { style: "text-align:center" },
                        template: '# if(Active) {# <i class="fa fa-check" aria-hidden="true" style="color:green"></i> #} else {# <i class="fa fa-times" aria-hidden="true" style="color:red"></i> #} #'
                    }, {
                        field: "StoreName",
                        title: "@T("Admin.Promotions.NewsLetterSubscriptions.Fields.Store")",
                        width: 150
                    }, {
                        field: "Categories",
                         title: "@T("Admin.Promotions.NewsLetterSubscriptions.Fields.Categories")",
                         width: 300
                    }, {
                        field: "CreatedOn",
                        title: "@T("Admin.Promotions.NewsLetterSubscriptions.Fields.CreatedOn")",
                        width: 150,
                        type: "string"
                    }, {
                        command: [
                            {
                                name: "edit",
                                text: {
                                    edit: "@T("Admin.Common.Edit")",
                                    update: "@T("Admin.Common.Update")",
                                    cancel: "@T("Admin.Common.Cancel")"
                                }
                            }, {
                                name: "destroy",
                                text: "@T("Admin.Common.Delete")"
                            }
                        ],
                        width: 200
                    }
                ]
            });
        });
    </script>

    <script type="text/javascript">
        $(document).ready(function() {
            $('#search-subscriptions').click(function() {
                var grid = $('#newsletter-subscriptions-grid').data('kendoGrid');
                grid.dataSource.page(1); //new search. Set page size to 1
                //grid.dataSource.read(); we already loaded the grid above using "page" function
                return false;
            });

            $("#@Html.FieldIdFor(model => model.SearchEmail)").keydown(function(event) {
                if (event.keyCode == 13) {
                    $("#search-subscriptions").click();
                    return false;
                }
            });
        });

        function additionalData() {
            var categoryIds = [];
            if($('#@Html.FieldIdFor(model => model.SearchCategoryIds)').val()!=null){
                categoryIds = $('#@Html.FieldIdFor(model => model.SearchCategoryIds)').val();
            }


            var data = {
                SearchCategoryIds: categoryIds,
                ActiveId: $('#@Html.FieldIdFor(model => model.ActiveId)').val(),
                StoreId: $('#@Html.FieldIdFor(model => model.StoreId)').val(),
                SearchEmail: $('#@Html.FieldIdFor(model => model.SearchEmail)').val()
            };
            addAntiForgeryToken(data);
            return data;
        }
    </script>
</form>

    @*import emails form*@
    <div id="importcsv-window" style="display:none;">
        <form asp-controller="NewsLetterSubscription" asp-action="ImportCsv" method="post" enctype="multipart/form-data">
            
            <table style="text-align:left;">
                <tr>
                    <td>
                        @T("Admin.Common.CsvFile"):
                    </td>
                    <td>
                        <input type="file" id="importcsvfile" name="importcsvfile" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" class="k-button" value="@T("Admin.Common.ImportFromCsv")" />
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <script type="text/javascript">
    $(document).ready(function () {
        $("#importcsv").click(function (e) {
            e.preventDefault();
            var window = $("#importcsv-window");
            if (!window.data("kendoWindow")) {
                window.kendoWindow({
                    modal: true,
                    width: "400px",
                    title: "@T("Admin.Common.ImportFromCsv")",
                    actions: ["Close"]
                });
            }
            window.data('kendoWindow').center().open();
        });
    });
    </script>
